package com.github.prontera.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.common.filter.IgnoreAuth;
import com.github.prontera.domain.Client;
import com.github.prontera.domain.Consume;
import com.github.prontera.domain.enums.ConsumeLevelTypeEnum;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.model.response.Response;
import com.github.prontera.service.ClientService;
import com.github.prontera.service.ConsumeService;
import com.github.prontera.util.Page;

import io.swagger.annotations.ApiOperation;

/**
 * 财务管理
 * @author Mengxiangdong
 */
@RestController
@RequestMapping(value = ApiVersion.MANAGE_V1_PREFIX + "/finance", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public class FinanceController {

	@Autowired
	private ConsumeService consumeService;
	
	@Autowired
	private ClientService clientService;

	@IgnoreAuth
	@RequestMapping(value = "financeList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "财务总计" , notes = "财务总计")
	public ObjectDataResponse<Page<Consume>> financeList(@RequestBody Consume consume){
		consume.setOrderBy("U_CONSUME.create_time desc");
		Page<Consume> datas=consumeService.page(consume); 
		return new ObjectDataResponse<Page<Consume>>(datas);
	}
	
	@IgnoreAuth
	@RequestMapping(value = "financeList2", method = RequestMethod.POST)
	@ApiOperation(value = "财务总计" , notes = "财务总计")
	public ObjectDataResponse<String> financeList2(){
		System.out.println("ok==================");
		return new ObjectDataResponse<String>("123");
	}
	
	@IgnoreAuth
	@RequestMapping(value = "consume", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "消费记录列表" , notes = "消费记录列表")
	public ObjectDataResponse<Page<Consume>> consume(@RequestBody Consume consume){
		consume.setOrderBy("U_CONSUME.create_time desc");
		Page<Consume> datas=consumeService.page(consume);
		return new ObjectDataResponse<Page<Consume>>(datas);
	}
	
	@IgnoreAuth
	@RequestMapping(value = "deposit", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "提现记录列表" , notes = "提现记录列表")
	public ObjectDataResponse<Page<Consume>> deposit(@RequestBody Consume consume){
		consume.setLevelType(ConsumeLevelTypeEnum.BALANCE);
		consume.setOrderBy("U_CONSUME.create_time desc");
		Page<Consume> datas=consumeService.page(consume);
		return new ObjectDataResponse<Page<Consume>>(datas);
	}

	@ApiOperation(value = "提现处理", notes = "提现处理")
	@RequestMapping(value = "/updateDeposit", method = RequestMethod.POST)
	@ResponseBody
	public Response updateDeposit(@RequestBody Consume consume) {
		int success = consumeService.update(consume);
		
		//同意提现
		if (consume.getStatus()==4 && success == 1) {
			Client client=new Client();
			client.setId(consume.getClientId());
			client.setLocakedCashAmount(new BigDecimal(0));
			clientService.update(client);
		}
		
		//拒绝提现
		if(consume.getStatus()==3 && success ==1 ){
			Client client=	clientService.get(consume.getClientId());
			client.setId(consume.getClientId());
			client.setAmount(client.getAmount().add(client.getLocakedCashAmount()));
			client.setLocakedCashAmount(new BigDecimal(0));
			clientService.update(client);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "提现失败！");
		}
		return new ObjectDataResponse<Object>(map);
	}
	
	 

}
